﻿using FastData;
using M6R小圆柱注液机.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace M6R小圆柱注液机.Pages
{
    public partial class PointCheck : Form
    {
        /// <summary>
        /// 误差值
        /// </summary>
        public static float checkError;
        /// <summary>
        /// 砝码重量
        /// </summary>
        public static float weightValue;

        float FrontWeight_1CheckResult;//1号电子秤结果
        float FrontWeight_2CheckResult;//2号电子秤结果
        float BehindWeight_3CheckResult;//3号电子秤结果
        float BehindWeight_4CheckResult;//4号电子秤结果

        public PointCheck()
        {
            InitializeComponent();
        }

        private void 点检_Load(object sender, EventArgs e)
        {
            checkError = Convert.ToSingle(SaveStatic.ReadIni(GlobalValues.PathVariable.fileCommonPara, "CheckOut_Para", "CheckError"));
            weightValue = Convert.ToSingle(SaveStatic.ReadIni(GlobalValues.PathVariable.fileCommonPara, "CheckOut_Para", "WeightValue"));

            tbCheckError.Text = checkError.ToString("0.00");
            tbWeightValue.Text = weightValue.ToString("0.00");
            ///////////////////////////////////////////////////////////////以下判断是否点检

            btnFrontWeightCheck.Enabled = false;
            btnBehindWeightCheck.Enabled = false;
            btnReportData.Enabled = false;
            btnNext.Enabled = false;
            lbUpLoading.Visible = false;
        }


        /// <summary>
        /// 清零
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCheckReset_Click(object sender, EventArgs e)
        {
            btnFrontWeightCheck.Enabled = true;
            btnBehindWeightCheck.Enabled = false;
            btnReportData.Enabled = false;
            btnNext.Enabled = false;

            lbWeight_1Result.Text = "-";
            lbWeight_1Result.BackColor = Color.Transparent;
            lbWeight_2Result.Text = "-";
            lbWeight_2Result.BackColor = Color.Transparent;
            lbWeight_3Result.Text = "-";
            lbWeight_3Result.BackColor = Color.Transparent;
            lbWeight_4Result.Text = "-";
            lbWeight_4Result.BackColor = Color.Transparent;
        }

        /// <summary>
        /// 前称重点检
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFrontWeightCheck_Click(object sender, EventArgs e)
        {
            //执行点检过程
            FrontWeight_1CheckResult = 50.001f;//1号电子秤结果
            FrontWeight_2CheckResult = 50.002f;//2号电子秤结果
            bool weight_1Result = (Math.Abs(FrontWeight_1CheckResult - weightValue)) < checkError;
            bool weight_2Result = (Math.Abs(FrontWeight_2CheckResult - weightValue)) < checkError;

            //1号称
            if (weight_1Result)
            {
                lbWeight_1Result.Text = FrontWeight_1CheckResult.ToString();
                lbWeight_1Result.BackColor = Color.Green;
            }
            else
            {
                lbWeight_1Result.Text = FrontWeight_1CheckResult.ToString();
                lbWeight_1Result.BackColor = Color.Red;
            }

            //2号称
            if (weight_2Result)
            {
                lbWeight_2Result.Text = FrontWeight_2CheckResult.ToString();
                lbWeight_2Result.BackColor = Color.Green;
            }
            else
            {
                lbWeight_2Result.Text = FrontWeight_2CheckResult.ToString();
                lbWeight_2Result.BackColor = Color.Red;
            }

            if (eventFrontWeightCheckResult != null) { eventFrontWeightCheckResult(FrontWeight_1CheckResult, FrontWeight_2CheckResult, weight_1Result, weight_2Result); }

            if (weight_1Result && weight_2Result)
            {
                //两个电子秤都验证通过，再解锁后称重电子秤的验证
                btnBehindWeightCheck.Enabled = true;
            }
        }

        /// <summary>
        /// 后称重点检
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBehindWeightCheck_Click(object sender, EventArgs e)
        {
            BehindWeight_3CheckResult = 50.006f;//3号电子秤结果
            BehindWeight_4CheckResult = 50.007f;// 4号电子秤结果
            bool weight_3Result = (Math.Abs(BehindWeight_3CheckResult - weightValue)) < checkError;
            bool weight_4Result = (Math.Abs(BehindWeight_4CheckResult - weightValue)) < checkError;

            //3号称
            if (weight_3Result)
            {
                lbWeight_3Result.Text = BehindWeight_3CheckResult.ToString();
                lbWeight_3Result.BackColor = Color.Green;
            }
            else
            {
                lbWeight_3Result.Text = BehindWeight_3CheckResult.ToString();
                lbWeight_3Result.BackColor = Color.Red;
            }

            //4号称
            if (weight_4Result)
            {
                lbWeight_4Result.Text = BehindWeight_4CheckResult.ToString();
                lbWeight_4Result.BackColor = Color.Green;
            }
            else
            {
                lbWeight_4Result.Text = BehindWeight_4CheckResult.ToString();
                lbWeight_4Result.BackColor = Color.Red;
            }

            if (eventBehindWeightCheckResult != null) { eventBehindWeightCheckResult(BehindWeight_3CheckResult, BehindWeight_4CheckResult, weight_3Result, weight_4Result); }

            if (weight_3Result && weight_4Result)
            {
                //两个电子秤都验证通过，解锁“数据上传”和“下一步”
                btnReportData.Enabled = true;
                btnNext.Enabled = true;
            }
        }

        /// <summary>
        /// 数据上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReportData_Click(object sender, EventArgs e)
        {
            string path = "E:\\Configuration\\点检结果\\" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".txt";
            string[] value = { "1号电子秤校验结果：" + FrontWeight_1CheckResult, "2号电子秤校验结果：" + FrontWeight_2CheckResult, "3号电子秤校验结果：" + BehindWeight_3CheckResult, "4号电子秤校验结果：" + BehindWeight_4CheckResult };
            SaveStatic.SaveTxt(path, value, true);

            lbUpLoading.Visible = true;
            lbUpLoading.Text = "数据上传成功";
            lbUpLoading.ForeColor = Color.Green;
            GlobalValues.log_Helper.DispProcess("电子秤点检结果已上传", "Info", true);
        }

        /// <summary>
        /// 下一步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNext_Click(object sender, EventArgs e)
        {
            this.Close();
        }




        /// <summary>
        /// 前称重结果委托
        /// </summary>
        /// <param name="result_1">1号称称重值</param>
        /// <param name="result_2">2号称称重值</param>
        /// <param name="value1">1号称称重结果是否合格？</param>
        /// <param name="value2">2号称称重结果是否合格？</param>
        public delegate void delegateFrontWeightCheckResult(float result_1, float result_2, bool value1, bool value2);
        /// <summary>
        /// 1、2 号称反馈结果的事件（前电子秤）
        /// </summary>
        public event delegateFrontWeightCheckResult eventFrontWeightCheckResult;

        /// <summary>
        /// 后称重结果委托
        /// </summary>
        /// <param name="result_3">3号称称重值</param>
        /// <param name="result_4">4号称称重值</param>
        /// <param name="value1">3号称称重结果是否合格？</param>
        /// <param name="value2">4号称称重结果是否合格？</param>
        public delegate void delegateBehindWeightCheckResult(float result_3, float result_4, bool value1, bool value2);
        /// <summary>
        /// 3、4号称反馈结果的事件(后电子秤)
        /// </summary>
        public event delegateBehindWeightCheckResult eventBehindWeightCheckResult;


    }
}
